package com.bossly.contactsflag.activities;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bossly.contactsflag.R;

public class ContactsAdapter extends CursorAdapter {

	private static final String ACCOUNT_TYPE = "com.bossly.contactflag.contact";

	static String[][] accounts = new String[][] { { "com.google", "Google" },
			{ "com.twitter.android.auth.login", "Twitter" },
			{ "com.facebook.auth.login", "Facebook" } };

	public int contactId;
	private Context _context;

	public ContactsAdapter(Context context, Cursor c, int contact) {
		super(context, c);
		// TODO Auto-generated constructor stub
		contactId = contact;
		_context = context;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {

		ContactTag contact = (ContactTag) view.getTag();
		contact.isContactType = false;

		int rawcontactId = cursor
				.getInt(cursor.getColumnIndex(RawContacts._ID));

		String where = Data.RAW_CONTACT_ID + " = " + rawcontactId + " AND "
				+ Data.MIMETYPE + "=='" + Photo.CONTENT_ITEM_TYPE + "'";

		ContentResolver c = _context.getContentResolver();
		Cursor cc = c.query(Data.CONTENT_URI, null, where, null, null);
		Bitmap photo = null;

		if (cc.moveToFirst()) {
			int index = cc.getColumnIndexOrThrow(Photo.PHOTO);
			byte[] bytes = cc.getBlob(index);

			if (bytes != null)
				photo = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		}

		cc.close();

		where = RawContacts._ID + " = " + rawcontactId + " AND "
				+ RawContacts.ACCOUNT_TYPE + "=='" + ACCOUNT_TYPE + "'";

		Cursor cursor1 = c.query(RawContacts.CONTENT_URI, null, where, null,
				null);

		// get exist raw contact
		if (cursor1.moveToFirst()) {
			contact.text2.setText(R.string.litst_item1_contact_name);
			contact.text1
					.setText(R.string.list_item2_account_not_syncronizable);
			contact.isContactType = true;
		} else {

			String acc_type = cursor.getString(cursor
					.getColumnIndex(RawContacts.ACCOUNT_TYPE));

			for (int i = 0; i < accounts.length; i++) {
				if (acc_type.equalsIgnoreCase(accounts[i][0])) {
					acc_type = accounts[i][1];
					break;
				}
			}

			// set display name
			String name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			contact.text1.setText(String.format("%s %s", _context
					.getResources().getString(R.string.list_item2_account),
					name));

			contact.text2.setText(acc_type);
		}

		contact.contactId = contactId;
		contact.rawcontactId = rawcontactId;
		contact.photo = photo;
		contact.badge.setImageBitmap(photo);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {

		View view = LayoutInflater.from(context).inflate(
				R.layout.item_photo_contact, null);

		// set tag
		ContactTag contact = new ContactTag();
		contact.text1 = (TextView) view.findViewById(android.R.id.text1);
		contact.text2 = (TextView) view.findViewById(android.R.id.text2);
		contact.badge = (ImageView) view.findViewById(R.id.quickContact);
		view.setTag(contact);

		return view;
	}
}
